package lb.io.client;

import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup;
import io.grpc.NameResolver;

import javax.annotation.Nullable;
import java.net.InetSocketAddress;
import java.util.ArrayList;

public class LocalNameResolver extends NameResolver {
    private final String[] endpoints;

    public LocalNameResolver(String[] endpoints) {
        this.endpoints = endpoints;
    }

    @Override
    public String getServiceAuthority() {
        return "none";
    }

    @Override
    public void start(Listener listener) {
        ArrayList<EquivalentAddressGroup> addressGroups = new ArrayList<>();
        for (String item : endpoints) {
            String[] line = item.split(":", 2);
            if (line.length < 2) {
                continue;
            }
            addressGroups.add(new EquivalentAddressGroup(new InetSocketAddress(line[0], Integer.parseInt(line[1]))));
        }
        listener.onAddresses(addressGroups, Attributes.EMPTY);
    }

    @Override
    public void shutdown() {

    }
}

